﻿using Coffee_Restaurant.DAL;
using Coffee_Restaurant.Database;
using System;
using System.Linq;
using System.Windows.Forms;
using Coffee_Restaurant.Forms.AreaTable;
using MetroFramework;

namespace Coffee_Restaurant.UserControls
{
    public partial class ManagerAreaNumber : UserControl
    {
        private int RowIndex { get; set; }
        private int ColumnIndex { get; set; }

        private int AreaId { get; set; }
        private string NumberTable { get; set; }

        private Button buttonTableNumber;
        public ManagerAreaNumber()
        {
            InitializeComponent();

        }



        private void btn_Click(object sender, EventArgs e)
        {
            var button = sender as Button;
            if (button != null)
                switch (button.Name)
                {
                    case "btnAddArea":
                        var areaAdd = new AreaAddEdit { lblTitle = { Text = "Thêm khu vực bàn" } };
                        areaAdd.BtnAddArea += areaAdd_BtnAddArea;
                        areaAdd.ShowDialog();
                        break;
                    case "btnEditArea":
                        if (AreaId > 0)
                        {

                            using (var db = new DatabaseEntities())
                            {
                                var model = db.Areas.Find(AreaId);
                                var areaEdit = new AreaAddEdit
                                {
                                    lblTitle = { Text = "Sửa khu vực bàn" },
                                    AreaId = AreaId,
                                    btnSave = { Name = "btnSaveEdit" },
                                    txtNameArea = { Text = model.Name },
                                    txtDescription = { Text = model.Description }
                                };
                                areaEdit.BtnEditArea += areaEdit_BtnEditArea;
                                areaEdit.ShowDialog();
                            }
                        }
                        else
                        {
                            //CustomMessageBox.Show("Bạn chưa chọn khu vực cần sửa", "", "alert");
                        }

                        break;
                    case "btnDeleteArea":
                        if (AreaId > 0)
                        {
                            DialogResult dialogResult = CustomMessageBox.Show("Bạn chắc chắn muốn xóa bàn này", "", "confirm", MetroColorStyle.Default);
                            if (dialogResult == DialogResult.Yes)
                            {
                                var areaDa = new AreaDa();
                                if (areaDa.DeleteArea(AreaId) > 0)
                                {
                                    //remove node tree
                                    treeViewArea.Nodes[AreaId.ToString()].Remove();
                                }
                            }
                        }
                        else
                        {
                            CustomMessageBox.Show("Bạn chưa chọn khu vực cần xóa", "", "alert", MetroColorStyle.Default);
                        }
                        break;
                    case "btnAddTable":
                        var tableAdd = new TableAddEdit { lblTitle = { Text = "Thêm số bàn" } };
                        tableAdd.AreaId = AreaId; //set areaid de cho combobox su dung default
                        tableAdd.BtnAddTable += tableAdd_BtnAddTable;
                        tableAdd.ShowDialog();
                        break;
                    case "btnEditTable":
                        if (!string.IsNullOrEmpty(NumberTable))
                        {
                            using (var db = new DatabaseEntities())
                            {
                                //tim kiem theo ten ban
                                var model = db.Tables.FirstOrDefault(t => t.Name == NumberTable);
                                if (model != null)
                                {
                                    var tableEdit = new TableAddEdit()
                                    {
                                        lblTitle = { Text = "Sửa số bàn" },
                                        numberTable = NumberTable,
                                        AreaId = model.AreaId,
                                        btnSave = { Name = "btnSaveEdit" },
                                        txtName = { Text = model.Name },
                                        txtDescription = { Text = model.Description }
                                    };

                                    tableEdit.BtnEditTable += tableEdit_BtnEditTable;
                                    tableEdit.ShowDialog();
                                }
                            }
                        }
                        else
                        {
                            CustomMessageBox.Show("Bạn chưa chọn bàn cần sửa", "", "alert", MetroColorStyle.Default);
                        }
                        break;
                    case "btntableNumber":
                        NumberTable = button.Text;
                        break;
                    case "btnDeleteTable":

                        if (!string.IsNullOrEmpty(NumberTable))
                        {
                            DialogResult dialogResult = CustomMessageBox.Show("Bạn chắc chắn muốn xóa bàn này", "", "confirm", MetroColorStyle.Default);
                            if (dialogResult == DialogResult.Yes)
                            {
                                var tableDa = new TableDa();
                                if (tableDa.DeleteTable(NumberTable) > 0)
                                {
                                    //remove
                                    foreach (var control in flowLayoutPanelTable.Controls.Find("btntableNumber", true))
                                    {
                                        if (control.Text == NumberTable)
                                        {
                                            flowLayoutPanelTable.Controls.Remove(control);
                                        }
                                    }
                                }
                            }

                        }
                        else
                        {
                            CustomMessageBox.Show("Bạn chưa chọn bàn cần xóa", "", "alert", MetroColorStyle.Default);
                        }
                        break;
                }
        }

        void tableEdit_BtnEditTable(string oldname, string areaId, string name)
        {
            TreeNode[] node = treeViewArea.Nodes.Find(areaId, true);
            //load treeview
            treeViewArea.SelectedNode = node[0];
            foreach (var control in flowLayoutPanelTable.Controls.Find("btntableNumber", true))
            {
                if (control.Text == oldname)
                {
                    control.Text = name;
                }
            }

        }

        void tableAdd_BtnAddTable(string name)
        {
            try
            {
                if (name != null)
                {

                    AddTableNumber(name);
                }
            }
            catch (Exception)
            {

            }
        }

        void AddTableNumber(string name)
        {
            buttonTableNumber = new Button();
            buttonTableNumber.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonTableNumber.ForeColor = System.Drawing.Color.Black;
            buttonTableNumber.Image = global::Coffee_Restaurant.Properties.Resources.table;
            buttonTableNumber.Location = new System.Drawing.Point(3, 3);
            buttonTableNumber.Name = "btntableNumber";
            buttonTableNumber.Size = new System.Drawing.Size(60, 60);
            buttonTableNumber.Margin = new Padding(3, 3, 20, 10);
            buttonTableNumber.TabIndex = 0;
            buttonTableNumber.Text = name;
            buttonTableNumber.UseVisualStyleBackColor = true;
            buttonTableNumber.FlatStyle = FlatStyle.Flat;
            buttonTableNumber.FlatAppearance.BorderColor = System.Drawing.Color.Brown;
            buttonTableNumber.Click += btn_Click;

            flowLayoutPanelTable.Controls.Add(buttonTableNumber);
        }

        //edit node tree
        void areaEdit_BtnEditArea(string key, string name)
        {
            try
            {
                if (name != null && key != null)
                {
                    treeViewArea.Nodes[key].Text = name;
                }
            }
            catch (Exception)
            {

            }
        }

        //add new node tree
        public void areaAdd_BtnAddArea(string key, string name)
        {
            try
            {
                if (name != null && key != null)
                {
                    treeViewArea.Nodes.Add(key, name);
                }
            }
            catch (Exception)
            {

            }
        }

        private void ManagerAreaNumber_Load(object sender, EventArgs e)
        {
            using (var db = new DatabaseEntities())
            {
                var model = db.sp_AreasDataSoure().ToList();
                foreach (var spAreasDataSoureResult in model)
                {
                    treeViewArea.Nodes.Add(spAreasDataSoureResult.AreaId.ToString(), spAreasDataSoureResult.Name + " (0)");
                }

                var modelTable = db.Tables.ToList();
                foreach (var table in modelTable)
                {
                    AddTableNumber(table.Name);
                }

                //dataGridViewArea.AllowUserToAddRows = true;
                //dataGridViewArea.DataSource = model;
                //dataGridViewArea.Columns[0].HeaderText = "STT";
                //dataGridViewArea.Columns[0].ReadOnly = true;
                //dataGridViewArea.Columns[0].Width = 50;
                //dataGridViewArea.Columns[0].Name = "Id";

                //dataGridViewArea.Columns[1].HeaderText = "Tên khu vực";
                //dataGridViewArea.Columns[1].Name = "NameArea";
            }
        }

        private void treeViewArea_AfterSelect(object sender, TreeViewEventArgs e)
        {
            AreaId = Convert.ToInt32(treeViewArea.Nodes[e.Node.Index].Name);
            //load table thuoc area nay
            using (var db = new DatabaseEntities())
            {
                var model = db.Tables.Where(t => t.AreaId == AreaId).ToList();
                flowLayoutPanelTable.Controls.Clear();
                foreach (var table in model)
                {
                    AddTableNumber(table.Name);
                }
            }

        }

    }
}
